﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class BundleDependencies : EditorWindow
{
    Vector2 m_Scroll = Vector2.zero;
    Vector2 m_Scroll2 = Vector2.zero;

    static BundleGroup m_CurrentBundle;
    static public void ShowDependency(BundleGroup bundle)
    {
        BundleDependencies window = GetWindow<BundleDependencies>(false, "Dependencies", true);
        window.maxSize = new Vector2(300f, 300f);
        window.minSize = new Vector2(300f, 300f);
        m_CurrentBundle = bundle;
    }

    void OnGUI()
    {
        BuildAssetBundle.HighlightLine(Color.black, 20f);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("BundlesList", EditorStyles.boldLabel, GUILayout.Width(120f));
        GUILayout.Space(50f);
        EditorGUILayout.LabelField("Dependency", EditorStyles.boldLabel, GUILayout.Width(120f));
        EditorGUILayout.EndHorizontal();

        if (m_CurrentBundle == null)
            return;

        EditorGUILayout.BeginHorizontal();
        BuildAssetBundle.HighBackgroud(new Color(0.2f, 0.2f, 0.2f, 1.0f), new Rect(2f, 20f, 140f, 255f));
        BuildAssetBundle.HighBackgroud(new Color(0.2f, 0.2f, 0.2f, 1.0f), new Rect(155f, 20f, 140f, 255f));

        GUILayout.Space(3f);
        m_Scroll = EditorGUILayout.BeginScrollView(m_Scroll, GUILayout.Width(140f), GUILayout.Height(255f));
        EditorGUILayout.BeginVertical();
        Dictionary<string, BundleGroup> bundlesList = BuildAssetBundle.m_BundleList;
        if (bundlesList != null && bundlesList.Count > 0)
        {
            foreach (BundleGroup bundle in bundlesList.Values)
            {
                if (bundle == m_CurrentBundle)
                    continue;

                if (m_CurrentBundle.m_Dependencies.Contains(bundle.GetId()))
                    continue;

                if (GUILayout.Button(bundle.m_BundleName))
                {
                    m_CurrentBundle.m_Dependencies = bundle.GetId();
                }
            }
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();

        GUILayout.Space(15f);
        m_Scroll2 = EditorGUILayout.BeginScrollView(m_Scroll2, GUILayout.Width(140f), GUILayout.Height(255f));
        EditorGUILayout.BeginVertical();
        if (m_CurrentBundle != null && !string.IsNullOrEmpty(m_CurrentBundle.m_Dependencies))
        {
            if (GUILayout.Button(m_CurrentBundle.m_Dependencies))
            {
                m_CurrentBundle.m_Dependencies = "";
            }
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndHorizontal();
        GUI.Label(new Rect(0, Screen.height - 40f, 400f, 30f), "CurrentBundle: " + m_CurrentBundle.m_BundleName);
    }
}